<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        function myaxios({method,url,params,data}) {
            let arr = []
            for (const key in params) {
               arr.push(`${key}=${params[key]}`)
            }
            const xhr = new XMLHttpRequest()
            xhr.open(method,url+'?'+arr.join('&'))
            if (data instanceof FormData) {
                xhr.send(data)
            }else if(data instanceof Object){
                xhr.setRequestHeader('Content-Type', 'application/json')
                xhr.send(JSON.stringify(data))
            }else if (data instanceof String) {
                xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
            xhr.send(data)
            }else{
                xhr.send()
            }
            return new Promise((resolve, reject) => {
                xhr.addEventListener('load',()=>{
                    if (xhr.status >= 200 && xhr.status < 300) {
                        resolve(JSON.parse(xhr.response))
                    }else if (xhr.status >= 300) {
                        reject(JSON.parse(xhr.response))
                    }
                })
            })
        }

        myaxios({
      method: 'POST',
      url: 'http://www.itcbc.com:3006/api/addbook',
      data: {
        bookname: '西游记',
        author: '吴承恩',
        publisher: '北京出版社',
        appkey: 'laotang1234'
      }
    }).then((res)=>{
        console.log(res);
    })
    </script>
</body>
</html>